package machine;

import java.awt.*;
import java.awt.event.*;
import java.io.PrintStream;
import java.util.Vector;
import javax.swing.*;
import javax.swing.text.JTextComponent;


class Machine extends Thread implements ActionListener
{

    static MainMemory mem;
    static RegisterBank reg;
    static String PC;
    static String IR;
    static boolean running;
    static MemoryGUI memGUI;
    static RegisterBankGUI regGUI;
    static JTextField PCGUI;
    static JTextField IRGUI;
    static JTextField asmInstGUI;
    static JTextField mlInstGUI;
    static JButton reset;
    static JButton step;
    static JButton run;
    static JButton load;
    static JTextField filename;
    static JTextField consoleIn;
    static JTextField consoleOut;
    static JProgressBar runningBarGUI;
    static Toolkit GUIToolkit;

    private static final String ZERO = "00000000000000000000000000000000";
    private static final String HALT = "11111111111111111111111111111111";
    private static final String BREAK = "00001111000011110000111100001111";
    private static final String ADD = "00100011";
    private static final String SUB = "00101111";
    private static final String AND = "00100010";
    private static final String OR = "00100001";
    private static final String LOAD = "01000001";
    private static final String STORE = "01000010";
    private static final String NOT = "01100100";
    private static final String SHL = "01100010";
    private static final String SHR = "01100001";
    private static final String LOADi = "10010011";
    private static final String LOADUi = "10011001";
    private static final String ADDi = "10110011";
    private static final String SUBi = "10111111";
    private static final String ANDi = "10110010";
    private static final String ORi = "10110001";
    private static final String BRANCH = "11000001";
    private static final String BZERO = "11100001";
    private static final String BNEG = "11100010";
    private static final String BODD = "11100011";
    private static final String JREG = "11100100";
    private static final String JAL = "10110100";
    private static final String CONSOLE_INPUT_ADDR = "11111111111111111111111111111100";
    private static final String CONSOLE_OUTPUT_ADDR = "11111111111111111111111111111000";


    public Machine()
    {
    }

    public Machine(String s)
    {
        mem = null;
        reg = new RegisterBank();
        PC = "00000000000000000000000000000000";
        IR = "00000000000000000000000000000000";
        running = false;
        setupGUI();
        if(s != null)
        {
            ((JTextComponent) (filename)).setText(s);
            load();
        }
    }

    private void setupGUI()
    {
        JFrame jframe = new JFrame("CS251 Machine Emulator");
        GUIToolkit = ((Window) (jframe)).getToolkit();
        Container container = jframe.getContentPane();
        addComponents(container);
        ((Window) (jframe)).addWindowListener(((java.awt.event.WindowListener) (new WindowAdapter() {

            public void windowClosing(WindowEvent windowevent)
            {
                System.exit(0);
            }

        })));
        ((Window) (jframe)).pack();
        ((Window) (jframe)).show();
    }

    private void addComponents(Container container)
    {
        container.setLayout(((java.awt.LayoutManager) (new BoxLayout(container, 0))));
        regGUI = new RegisterBankGUI(RegisterBank.numRegisters);
        container.add(((java.awt.Component) (regGUI)));
        JPanel jpanel = new JPanel();
        ((Container) (jpanel)).setLayout(((java.awt.LayoutManager) (new BoxLayout(((Container) (jpanel)), 1))));
        JPanel jpanel1 = new JPanel();
        ((Container) (jpanel1)).setLayout(((java.awt.LayoutManager) (new BoxLayout(((Container) (jpanel1)), 0))));
        step = new JButton("Step");
        ((AbstractButton) (step)).addActionListener(((ActionListener) (this)));
        ((AbstractButton) (step)).setEnabled(false);
        ((Container) (jpanel1)).add(((java.awt.Component) (step)));
        run = new JButton("Run");
        ((AbstractButton) (run)).addActionListener(((ActionListener) (this)));
        ((AbstractButton) (run)).setEnabled(false);
        ((Container) (jpanel1)).add(((java.awt.Component) (run)));
        reset = new JButton("Reset");
        ((AbstractButton) (reset)).addActionListener(((ActionListener) (this)));
        ((AbstractButton) (reset)).setEnabled(false);
        ((Container) (jpanel1)).add(((java.awt.Component) (reset)));
        load = new JButton("Load");
        ((AbstractButton) (load)).addActionListener(((ActionListener) (this)));
        ((Container) (jpanel1)).add(((java.awt.Component) (load)));
        ((Container) (jpanel)).add(((java.awt.Component) (jpanel1)));
        JPanel jpanel2 = new JPanel();
        ((Container) (jpanel2)).setLayout(((java.awt.LayoutManager) (new BoxLayout(((Container) (jpanel2)), 0))));
        JLabel jlabel = new JLabel("File: ");
        ((JComponent) (jlabel)).setFont(new Font("Monospaced", 0, 12));
        ((Container) (jpanel2)).add(((java.awt.Component) (jlabel)));
        filename = new JTextField(32);
        filename.setFont(new Font("Monospaced", 0, 12));
        ((JTextComponent) (filename)).setText("");
        ((Container) (jpanel2)).add(((java.awt.Component) (filename)));
        ((Container) (jpanel)).add(((java.awt.Component) (jpanel2)));
        runningBarGUI = new JProgressBar(0, 10);
        runningBarGUI.setString("Stopped");
        runningBarGUI.setStringPainted(true);
        ((Container) (jpanel)).add(((java.awt.Component) (runningBarGUI)));
        ((Container) (jpanel)).add(((java.awt.Component) (new JLabel("  "))));
        JLabel jlabel1 = new JLabel("Instruction Information");
        ((Container) (jpanel)).add(((java.awt.Component) (jlabel1)));
        JPanel jpanel3 = new JPanel();
        ((Container) (jpanel3)).setLayout(((java.awt.LayoutManager) (new BoxLayout(((Container) (jpanel3)), 0))));
        JLabel jlabel2 = new JLabel(" PC: ");
        ((JComponent) (jlabel2)).setFont(new Font("Monospaced", 0, 12));
        ((Container) (jpanel3)).add(((java.awt.Component) (jlabel2)));
        PCGUI = new JTextField(32);
        ((JTextComponent) (PCGUI)).setEditable(false);
        PCGUI.setFont(new Font("Monospaced", 0, 12));
        ((JTextComponent) (PCGUI)).setText(PC + " (0)");
        ((Container) (jpanel3)).add(((java.awt.Component) (PCGUI)));
        ((Container) (jpanel)).add(((java.awt.Component) (jpanel3)));
        JPanel jpanel4 = new JPanel();
        ((Container) (jpanel4)).setLayout(((java.awt.LayoutManager) (new BoxLayout(((Container) (jpanel4)), 0))));
        JLabel jlabel3 = new JLabel(" IR: ");
        ((JComponent) (jlabel3)).setFont(new Font("Monospaced", 0, 12));
        ((Container) (jpanel4)).add(((java.awt.Component) (jlabel3)));
        IRGUI = new JTextField(32);
        ((JTextComponent) (IRGUI)).setEditable(false);
        IRGUI.setFont(new Font("Monospaced", 0, 12));
        ((Container) (jpanel4)).add(((java.awt.Component) (IRGUI)));
        ((Container) (jpanel)).add(((java.awt.Component) (jpanel4)));
        JPanel jpanel5 = new JPanel();
        ((Container) (jpanel5)).setLayout(((java.awt.LayoutManager) (new BoxLayout(((Container) (jpanel5)), 0))));
        JLabel jlabel4 = new JLabel(" ML: ");
        ((JComponent) (jlabel4)).setFont(new Font("Monospaced", 0, 12));
        ((Container) (jpanel5)).add(((java.awt.Component) (jlabel4)));
        mlInstGUI = new JTextField(22);
        ((JTextComponent) (mlInstGUI)).setEditable(false);
        mlInstGUI.setFont(new Font("Monospaced", 0, 12));
        ((Container) (jpanel5)).add(((java.awt.Component) (mlInstGUI)));
        ((Container) (jpanel)).add(((java.awt.Component) (jpanel5)));
        JPanel jpanel6 = new JPanel();
        ((Container) (jpanel6)).setLayout(((java.awt.LayoutManager) (new BoxLayout(((Container) (jpanel6)), 0))));
        JLabel jlabel5 = new JLabel("ASM: ");
        ((JComponent) (jlabel5)).setFont(new Font("Monospaced", 0, 12));
        ((Container) (jpanel6)).add(((java.awt.Component) (jlabel5)));
        asmInstGUI = new JTextField(22);
        ((JTextComponent) (asmInstGUI)).setEditable(false);
        asmInstGUI.setFont(new Font("Monospaced", 0, 12));
        ((Container) (jpanel6)).add(((java.awt.Component) (asmInstGUI)));
        ((Container) (jpanel)).add(((java.awt.Component) (jpanel6)));
        ((Container) (jpanel)).add(((java.awt.Component) (new JLabel("  "))));
        JLabel jlabel6 = new JLabel("Console");
        ((Container) (jpanel)).add(((java.awt.Component) (jlabel6)));
        JPanel jpanel7 = new JPanel();
        ((Container) (jpanel7)).setLayout(((java.awt.LayoutManager) (new BoxLayout(((Container) (jpanel7)), 0))));
        JLabel jlabel7 = new JLabel(" Input: ");
        ((JComponent) (jlabel7)).setFont(new Font("Monospaced", 0, 12));
        consoleIn = new JTextField(20);
        ((Container) (jpanel7)).add(((java.awt.Component) (jlabel7)));
        ((Container) (jpanel7)).add(((java.awt.Component) (consoleIn)));
        ((Container) (jpanel)).add(((java.awt.Component) (jpanel7)));
        JPanel jpanel8 = new JPanel();
        ((Container) (jpanel8)).setLayout(((java.awt.LayoutManager) (new BoxLayout(((Container) (jpanel8)), 0))));
        JLabel jlabel8 = new JLabel("Output: ");
        ((JComponent) (jlabel8)).setFont(new Font("Monospaced", 0, 12));
        consoleOut = new JTextField(20);
        ((JTextComponent) (consoleOut)).setEditable(false);
        ((Container) (jpanel8)).add(((java.awt.Component) (jlabel8)));
        ((Container) (jpanel8)).add(((java.awt.Component) (consoleOut)));
        ((Container) (jpanel)).add(((java.awt.Component) (jpanel8)));
        memGUI = new MemoryGUI(mem);
        ((Container) (jpanel)).add(((java.awt.Component) (memGUI)));
        container.add(((java.awt.Component) (jpanel)));
    }

    public void actionPerformed(ActionEvent actionevent)
    {
        String s = actionevent.getActionCommand();
        if(s.equals("Step"))
        {
            if(!running)
            {
                runningBarGUI.setString("Running");
                ((AbstractButton) (reset)).setEnabled(false);
                ((AbstractButton) (step)).setEnabled(false);
                ((AbstractButton) (load)).setEnabled(false);
                step();
                ((AbstractButton) (reset)).setEnabled(true);
                ((AbstractButton) (step)).setEnabled(true);
                ((AbstractButton) (load)).setEnabled(true);
                runningBarGUI.setString("Stopped");
                updateGUI();
            }
        } else
        if(s.equals("Run"))
        {
            running = true;
            ((AbstractButton) (run)).setText("Stop");
            Machine machine = new Machine();
            ((Thread) (machine)).start();
        } else
        if(s.equals("Stop"))
        {
            running = false;
            ((AbstractButton) (run)).setText("Run");
        } else
        if(s.equals("Reset"))
        {
            reset();
        } else
        if(s.equals("Load"))
        {
            load();
        }
    }

    private void load()
    {
        mem = new MainMemory(((JTextComponent) (filename)).getText().trim());
        if(!mem.getError())
        {
            memGUI.setMem(mem);
            runningBarGUI.setMaximum(mem.getMem().size());
            reset();
        } else
        {
            ((JTextComponent) (filename)).setText("Error loading: " + ((JTextComponent) (filename)).getText());
            memGUI.setMem(((MainMemory) (null)));
        }
    }

    private void reset()
    {
        PC = "00000000000000000000000000000000";
        ((JTextComponent) (PCGUI)).setText(PC + " (0)");
        IR = "00000000000000000000000000000000";
        ((JTextComponent) (IRGUI)).setText("");
        ((JTextComponent) (asmInstGUI)).setText("");
        ((JTextComponent) (mlInstGUI)).setText("");
        runningBarGUI.setValue(0);
        running = false;
        ((AbstractButton) (run)).setText("Run");
        reg.reset();
        ((JTextComponent) (consoleIn)).setText("");
        ((JTextComponent) (consoleOut)).setText("");
        ((AbstractButton) (run)).setEnabled(true);
        ((AbstractButton) (step)).setEnabled(true);
        updateGUI();
    }

    public void run()
    {
        runningBarGUI.setString("Running");
        ((AbstractButton) (reset)).setEnabled(false);
        ((AbstractButton) (step)).setEnabled(false);
        ((AbstractButton) (load)).setEnabled(false);
        if(IR.equals("00001111000011110000111100001111"))
        {
            step();
        }
        for(; !IR.equals("11111111111111111111111111111111") && !IR.equals("00001111000011110000111100001111") && running; step()) { }
        runningBarGUI.setString("Stopped");
        running = false;
        if(IR.equals("11111111111111111111111111111111"))
        {
            ((AbstractButton) (run)).setText("Run");
            ((AbstractButton) (run)).setEnabled(false);
        }
        if(IR.equals("00001111000011110000111100001111"))
        {
            ((AbstractButton) (run)).setText("Run");
            ((AbstractButton) (step)).setEnabled(true);
        } else
        {
            ((AbstractButton) (step)).setEnabled(true);
        }
        ((AbstractButton) (reset)).setEnabled(true);
        ((AbstractButton) (load)).setEnabled(true);
        updateGUI();
    }

    public void step()
    {
        if(!IR.equals("11111111111111111111111111111111"))
        {
            runningBarGUI.setValue(Integer.parseInt(PC, 2));
            IR = mem.read(PC);
            ((JTextComponent) (IRGUI)).setText(IR);
            ((JTextComponent) (asmInstGUI)).setText(mem.getAsmComment(PC));
            ((JTextComponent) (mlInstGUI)).setText(mem.getMLComment(PC));
            try
            {
                if(execute())
                {
                    PC = binAdd(PC, "100");
                }
                ((JTextComponent) (PCGUI)).setText(PC + " (" + Integer.parseInt(PC, 2) + ")");
            }
            catch(Exception exception)
            {
                System.err.println("\007");
                System.err.println("Unrecoverable Error - Machine being reset.");
                reset();
            }
        }
    }

    private void updateGUI()
    {
        regGUI.set(reg);
        memGUI.update();
    }

    private boolean execute()
    {
        if(IR.startsWith("00100011"))
        {
            ADD();
        } else
        if(IR.startsWith("00101111"))
        {
            SUB();
        } else
        if(IR.startsWith("00100010"))
        {
            AND();
        } else
        if(IR.startsWith("00100001"))
        {
            OR();
        } else
        if(IR.startsWith("01000001"))
        {
            LOAD();
        } else
        if(IR.startsWith("01000010"))
        {
            STORE();
        } else
        if(IR.startsWith("01100100"))
        {
            NOT();
        } else
        if(IR.startsWith("01100010"))
        {
            SHL();
        } else
        if(IR.startsWith("01100001"))
        {
            SHR();
        } else
        if(IR.startsWith("10010011"))
        {
            LOADi();
        } else
        if(IR.startsWith("10011001"))
        {
            LOADUi();
        } else
        if(IR.startsWith("10110011"))
        {
            ADDi();
        } else
        if(IR.startsWith("10111111"))
        {
            SUBi();
        } else
        if(IR.startsWith("10110010"))
        {
            ANDi();
        } else
        if(IR.startsWith("10110001"))
        {
            ORi();
        } else
        {
            if(IR.startsWith("11000001"))
            {
                BRANCH();
                return false;
            }
            if(IR.startsWith("11100001"))
            {
                return BZERO();
            }
            if(IR.startsWith("11100010"))
            {
                return BNEG();
            }
            if(IR.startsWith("11100011"))
            {
                return BODD();
            }
            if(IR.startsWith("11100100"))
            {
                JREG();
                return false;
            }
            if(IR.startsWith("10110100"))
            {
                JAL();
                return false;
            }
            if(!IR.startsWith("00000000000000000000000000000000") && !IR.startsWith("11111111111111111111111111111111") && !IR.startsWith("00001111000011110000111100001111"))
            {
                System.out.println("Unknown Instruction: " + IR);
            }
        }
        return true;
    }

    private void ADD()
    {
        String s = IR.substring(8, 12);
        String s1 = IR.substring(12, 16);
        String s2 = IR.substring(16, 20);
        String s3 = reg.read(s);
        String s4 = reg.read(s1);
        String s5 = binAdd(s3, s4);
        reg.write(s2, s5);
    }

    private void SUB()
    {
        String s = IR.substring(8, 12);
        String s1 = IR.substring(12, 16);
        String s2 = IR.substring(16, 20);
        String s3 = reg.read(s);
        String s4 = reg.read(s1);
        String s5 = binSub(s3, s4);
        reg.write(s2, s5);
    }

    private void AND()
    {
        String s = IR.substring(8, 12);
        String s1 = IR.substring(12, 16);
        String s2 = IR.substring(16, 20);
        String s3 = reg.read(s);
        String s4 = reg.read(s1);
        String s5 = binAND(s3, s4);
        reg.write(s2, s5);
    }

    private void OR()
    {
        String s = IR.substring(8, 12);
        String s1 = IR.substring(12, 16);
        String s2 = IR.substring(16, 20);
        String s3 = reg.read(s);
        String s4 = reg.read(s1);
        String s5 = binOR(s3, s4);
        reg.write(s2, s5);
    }

    private void LOAD()
    {
        String s = IR.substring(8, 12);
        String s1 = IR.substring(12, 16);
        String s2 = IR.substring(16, 32);
        String s3 = binAdd(reg.read(s), s2);
        String s4;
        if(s3.equals("11111111111111111111111111111100"))
        {
            s4 = getFromConsole();
        } else
        {
            s4 = mem.read(s3);
        }
        reg.write(s1, s4);
    }

    private String getFromConsole()
    {
        String s = ((JTextComponent) (consoleIn)).getText();
        int i = 0;
        String s1 = "";
        for(; i < s.length() && s.charAt(i) != ' '; i++)
        {
            s1 = s1 + s.charAt(i);
        }

        if(s1.equals(""))
        {
            ((JTextComponent) (consoleIn)).setText("");
            s1 = "0";
        } else
        {
            ((JTextComponent) (consoleIn)).setText(s.substring(i, s.length()).trim());
            if(s1.charAt(0) == '-')
            {
                s1 = Integer.toBinaryString(Integer.parseInt(s1));
            } else
            {
                s1 = "0" + Integer.toBinaryString(Integer.parseInt(s1));
            }
        }
        return signExtend(s1);
    }

    private void STORE()
    {
        String s = IR.substring(8, 12);
        String s1 = IR.substring(12, 16);
        String s2 = IR.substring(16, 32);
        String s3 = binAdd(reg.read(s), s2);
        String s4 = reg.read(s1);
        if(s3.equals("11111111111111111111111111111000"))
        {
            Long long1 = Long.valueOf(s4, 2);
            String s5 = (new Integer((int)long1.longValue())).toString();
            writeToConsole(s5);
        } else
        {
            mem.write(s3, s4);
        }
    }

    private void writeToConsole(String s)
    {
        ((JTextComponent) (consoleOut)).setText(((JTextComponent) (consoleOut)).getText() + " " + s);
    }

    private void NOT()
    {
        String s = IR.substring(12, 16);
        String s1 = IR.substring(16, 20);
        String s2 = reg.read(s);
        String s3 = binNot(s2);
        reg.write(s1, s3);
    }

    private void SHL()
    {
        String s = IR.substring(12, 16);
        String s1 = IR.substring(16, 20);
        String s2 = reg.read(s);
        String s3 = binSHL(s2);
        reg.write(s1, s3);
    }

    private void SHR()
    {
        String s = IR.substring(12, 16);
        String s1 = IR.substring(16, 20);
        String s2 = reg.read(s);
        String s3 = binSHR(s2);
        reg.write(s1, s3);
    }

    private void LOADi()
    {
        String s = IR.substring(12, 16);
        String s1 = signExtend(IR.substring(16, 32));
        reg.write(s, extend(s1));
    }

    private void LOADUi()
    {
        String s = IR.substring(12, 16);
        String s1 = IR.substring(16, 32);
        String s2 = reg.read(s);
        s1 = s1 + s2.substring(16, 32);
        reg.write(s, s1);
    }

    private void ADDi()
    {
        String s = IR.substring(8, 12);
        String s1 = IR.substring(12, 16);
        String s2 = signExtend(IR.substring(16, 32));
        String s3 = reg.read(s);
        String s4 = binAdd(s3, s2);
        reg.write(s1, s4);
    }

    private void SUBi()
    {
        String s = IR.substring(8, 12);
        String s1 = IR.substring(12, 16);
        String s2 = signExtend(IR.substring(16, 32));
        String s3 = reg.read(s);
        String s4 = binSub(s3, s2);
        reg.write(s1, s4);
    }

    private void ANDi()
    {
        String s = IR.substring(8, 12);
        String s1 = IR.substring(12, 16);
        String s2 = signExtend(IR.substring(16, 32));
        String s3 = reg.read(s);
        String s4 = binAND(s3, s2);
        reg.write(s1, s4);
    }

    private void ORi()
    {
        String s = IR.substring(8, 12);
        String s1 = IR.substring(12, 16);
        String s2 = IR.substring(16, 32);
        String s3 = reg.read(s);
        String s4 = signExtend(binOR(s3, s2));
        reg.write(s1, s4);
    }

    private void BRANCH()
    {
        String s = signExtend(IR.substring(16, 32));
        PC = binAdd(PC, binSHL(binSHL(s)));
    }

    private boolean BZERO()
    {
        String s = IR.substring(12, 16);
        String s1 = signExtend(IR.substring(16, 32));
        if(reg.read(s).equals("00000000000000000000000000000000"))
        {
            PC = binAdd(PC, binSHL(binSHL(s1)));
            return false;
        } else
        {
            return true;
        }
    }

    private boolean BNEG()
    {
        String s = IR.substring(12, 16);
        String s1 = signExtend(IR.substring(16, 32));
        if(reg.read(s).charAt(0) == '1')
        {
            PC = binAdd(PC, binSHL(binSHL(s1)));
            return false;
        } else
        {
            return true;
        }
    }

    private boolean BODD()
    {
        String s = IR.substring(12, 16);
        String s1 = signExtend(IR.substring(16, 32));
        if(reg.read(s).endsWith("1"))
        {
            PC = binAdd(PC, binSHL(binSHL(s1)));
            return false;
        } else
        {
            return true;
        }
    }

    private void JREG()
    {
        String s = IR.substring(12, 16);
        PC = reg.read(s);
    }

    private void JAL()
    {
        String s = IR.substring(8, 12);
        String s1 = IR.substring(12, 16);
        String s2 = reg.read(s);
        reg.write(s1, binAdd(PC, "0100"));
        PC = reg.read(s);
    }

    private String signExtend(String s)
    {
        for(; s.length() < 32; s = s.charAt(0) + s) { }
        return s;
    }

    private String extend(String s)
    {
        for(; s.length() < 32; s = "0" + s) { }
        return s;
    }

    private String binAdd(String s, String s1)
    {
        int i = (int)Long.parseLong(s, 2);
        int j = (int)Long.parseLong(s1, 2);
        String s2 = Integer.toBinaryString(i + j);
        return extend(s2);
    }

    private String binSub(String s, String s1)
    {
        int i = (int)Long.parseLong(s, 2);
        int j = (int)Long.parseLong(s1, 2);
        String s2 = Integer.toBinaryString(i - j);
        return extend(s2);
    }

    private String binAND(String s, String s1)
    {
        int i = (int)Long.parseLong(s, 2);
        int j = (int)Long.parseLong(s1, 2);
        String s2 = Integer.toBinaryString(i & j);
        return extend(s2);
    }

    private String binOR(String s, String s1)
    {
        int i = (int)Long.parseLong(s, 2);
        int j = (int)Long.parseLong(s1, 2);
        String s2 = Integer.toBinaryString(i | j);
        return extend(s2);
    }

    private String binNot(String s)
    {
        int i = (int)Long.parseLong(s, 2);
        String s1 = Integer.toBinaryString(~i);
        return extend(s1);
    }

    private String binSHL(String s)
    {
        int i = (int)Long.parseLong(s, 2);
        String s1 = Integer.toBinaryString(i << 1);
        return extend(s1);
    }

    private String binSHR(String s)
    {
        int i = (int)Long.parseLong(s, 2);
        String s1 = Integer.toBinaryString(i >>> 1);
        return extend(s1);
    }

    public static void main(String args[])
    {
        Machine machine;
        if(args.length == 0)
        {
            machine = new Machine(((String) (null)));
        }
        if(args.length == 1)
        {
            machine = new Machine(args[0]);
        } else
        {
            printUsage();
        }
    }

    private static void printUsage()
    {
        System.out.println("java Machine [<mem image>]");
        System.out.println();
        System.out.println("   <mem image>: Memory image to load.");
    }
}
